package com.baizhi.app;


import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.baizhi.common.ResultCommon;
import com.baizhi.vo.Category;
import com.baizhi.service.AppService;
import com.baizhi.util.AliyunSms;
import com.baizhi.util.SecurityCode;
import com.baizhi.vo.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("yingx/app")
public class AppController {

    @Autowired
    private AppService appService;

    @RequestMapping("queryByReleaseTime")
    public ResultCommon queryByReleaseTime(){
        int count = 1;
        List<Video> videos = appService.queryVideo();
        for (Video video : videos) {
            video.setLikeCount(count++);
        }
        return ResultCommon.ok().setMessage("查询成功").setData(videos);
    }

    //查询所有一级
    @RequestMapping("queryAllCategory")
    public ResultCommon queryAllCategory(){
        List<Category> categories = appService.queryAllCategory(1);
        return ResultCommon.ok().setMessage("查询成功").setData(categories);
    }


    //验证码
    @RequestMapping("getPhoneCode")
    public ResultCommon getPhoneCode(String phone){

        String code = SecurityCode.getSecurityCode();
        Map<String, String> sms = AliyunSms.sms(phone,code);

        if(sms == null){
            /*ResultCommon r = ResultCommon.error();
            r.setMessage("验证码发送失败");
            return r;*/
            //链式编程
            return ResultCommon.error().setMessage("验证码发送失败");
        }else{
            if(sms.get("Code").equals("OK")){
               /* ResultCommon r = ResultCommon.ok();//status: 100  message:验证码发送成功  data:phone
                r.setData(phone);
                r.setMessage("验证码发送成功");
                return r;*/
               return ResultCommon.ok().setMessage("验证码发送成功").setData(phone);
            }else{
               /* ResultCommon r = ResultCommon.error();
                r.setMessage("验证码发送失败");
                return r;*/
                return ResultCommon.error().setMessage("验证码发送失败");
            }
        }
    }
}
